package com.pets.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pets.entity.Collect;
import com.pets.entity.DownloadRecord;
import com.pets.entity.FileInformation;
import com.pets.mapper.CollectMapper;
import com.pets.mapper.FileInformationMapper;
import com.pets.service.ICollectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pets.utils.Result;
import com.pets.viewModels.CollectViewModels;
import com.pets.viewModels.FileViewModels;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 收藏表 服务实现类
 * </p>
 *
 * @author 啦啦啦
 * @since 2024-04-09
 */
@Service
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect> implements ICollectService {

    @Autowired
    private CollectMapper collectMapper;
    
    @Autowired
    private FileInformationMapper fileInformationMapper;

    /**
     * 添加收藏
     */
    @Override
    public Result register(Integer fileId, Integer userId) {
        if(fileId == null || userId == null){
            System.out.println(fileId+"/"+userId);
            return Result.fail("参数不能为空");
        }
        LambdaQueryWrapper<Collect> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Collect::getFileId, fileId).eq(Collect::getUserId, userId);
        List<Collect> collectList = collectMapper.selectList(lqw);
        if (collectList != null && collectList.size() > 0) {
            return Result.fail("已经收藏过此商品！");
        }
        Collect collect = new Collect();
        collect.setFileId(fileId);
        collect.setUserId(userId);
        return collectMapper.insert(collect) > 0 ? Result.success("收藏成功！") : Result.fail("收藏失败！");
    }

    /**
     * 移除收藏
     */
    @Override
    public Result deleteById(Integer id) {
        return collectMapper.deleteById(id)>0?Result.success("移除成功！"):Result.fail("移除失败！");
    }

    /**
     * 分页查询
     */
    @Override
    public Result selectPage(Integer page, Integer pageSize, String fileName, Integer userId) {
        LambdaQueryWrapper<Collect> lqw = new LambdaQueryWrapper<>();
        lqw.eq(userId!=null,Collect::getUserId,userId);
        IPage<Collect> Page = new Page<>(page, pageSize);
        IPage<Collect> IPage = collectMapper.selectPage(Page, lqw);
        //查询收藏表
        IPage<CollectViewModels> newIPage = new Page<>();
        newIPage.setPages(IPage.getPages());
        newIPage.setCurrent(IPage.getCurrent());
        newIPage.setSize(IPage.getSize());
        newIPage.setTotal(IPage.getTotal());
        List<Collect> collectList = IPage.getRecords();
        if(collectList==null||collectList.size()<=0){
            return Result.success("查询成功！",newIPage);
        }
        //对象映射器
        ModelMapper modelMapper = new ModelMapper();
        List<CollectViewModels> collectViewModelsList = new ArrayList<>();
        for (Collect collect : collectList) {
            CollectViewModels collectViewModels = modelMapper.map(collect, CollectViewModels.class);
            FileInformation fileInformation = fileInformationMapper.selectById(collect.getFileId());
            if(fileInformation!=null){
                if (fileName!=null&& !fileName.isEmpty()){
                    if(!fileInformation.getFileName().contains(fileName)){
                        continue;
                    }
                }
                collectViewModels.setFileName(fileInformation.getFileName());
                collectViewModels.setFileAddress(fileInformation.getFileAddress());
                collectViewModels.setFileDetails(fileInformation.getFileDetails());
            }
            collectViewModelsList.add(collectViewModels);
        }
        newIPage.setRecords(collectViewModelsList);
        return Result.success("查询成功", newIPage);
    }
}
